package com.estacionamientos

import com.estacionamientos.dto.PromocionDTO

class PromocionCalendarController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
	def promocionCalendarService
	
    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [promocionCalendarInstanceList: PromocionCalendar.list(params), promocionCalendarInstanceTotal: PromocionCalendar.count()]
    }

    def create = {
        def promocionCalendarInstance = new PromocionCalendar()
        promocionCalendarInstance.properties = params
        return [promocionCalendarInstance: promocionCalendarInstance]
    }

    def save = {
		PromocionDTO promocion = new PromocionDTO();
		promocion.setCantidadLugares(4);
		promocion.setDescripcion("cualquiera");
		promocion.setDesde(new Date());
		promocion.setHoraDesde(13);
		promocion.setHoraHasta(14);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(promocion.getDesde());
		calendar.add(calendar.DATE,4);
		promocion.setHasta(calendar.getTime());
		PromocionCalendar promocionCalendarInstance = promocionCalendarService.createMethod(promocion);
		
//		PromocionCalendar promocionCalendarInstance = new PromocionCalendar(params)
//		promocionCalendarInstance.getEstacionamiento().getPromociones().add(promocionCalendarInstance)
        if (promocionCalendarInstance != null) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'promocionCalendar.label', default: 'PromocionCalendar'), promocionCalendarInstance.id])}"
            redirect(action: "show", id: promocionCalendarInstance.id)
        }
        else {
            render(view: "create", model: [promocionCalendarInstance: promocionCalendarInstance])
        }
    }

    def show = {
        def promocionCalendarInstance = PromocionCalendar.get(params.id)
        if (!promocionCalendarInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'promocionCalendar.label', default: 'PromocionCalendar'), params.id])}"
            redirect(action: "list")
        }
        else {
            [promocionCalendarInstance: promocionCalendarInstance]
        }
    }

    def edit = {
        def promocionCalendarInstance = PromocionCalendar.get(params.id)
        if (!promocionCalendarInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'promocionCalendar.label', default: 'PromocionCalendar'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [promocionCalendarInstance: promocionCalendarInstance]
        }
    }

    def update = {
        def promocionCalendarInstance = PromocionCalendar.get(params.id)
        if (promocionCalendarInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (promocionCalendarInstance.version > version) {
                    
                    promocionCalendarInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'promocionCalendar.label', default: 'PromocionCalendar')] as Object[], "Another user has updated this PromocionCalendar while you were editing")
                    render(view: "edit", model: [promocionCalendarInstance: promocionCalendarInstance])
                    return
                }
            }
            promocionCalendarInstance.properties = params
            if (!promocionCalendarInstance.hasErrors() && promocionCalendarInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'promocionCalendar.label', default: 'PromocionCalendar'), promocionCalendarInstance.id])}"
                redirect(action: "show", id: promocionCalendarInstance.id)
            }
            else {
                render(view: "edit", model: [promocionCalendarInstance: promocionCalendarInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'promocionCalendar.label', default: 'PromocionCalendar'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def promocionCalendarInstance = PromocionCalendar.get(params.id)
        if (promocionCalendarInstance) {
            try {
                promocionCalendarInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'promocionCalendar.label', default: 'PromocionCalendar'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'promocionCalendar.label', default: 'PromocionCalendar'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'promocionCalendar.label', default: 'PromocionCalendar'), params.id])}"
            redirect(action: "list")
        }
    }
}
